#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include "contact.h"
int check(struct contact* p);
void Load(struct contact* con)
{//conserve the data of last time
	FILE* pf = fopen("data.txt", "r");
	if (pf == NULL)
	{
		return;
	}
	struct People_Information tmp = { 0 };
	while (fread(&tmp, sizeof(struct People_Information), 1, pf))
	{
		check(con);
		con->date[con->sz] = tmp;
		con->sz++;
	}
	fclose(pf);
	pf = NULL;
}
void Init(struct contact* con)
{
	
	
	assert(con);
	
	con->date = (struct  People_Information*)malloc(PRIME*sizeof(struct People_Information));
	if (con->date == NULL)
	{
		perror("Init()");
		return;
	}
	con->sz = 0;
	con->capacity = PRIME;
}

 int check(struct contact* p)
{
	if (p->sz == p->capacity)
	{
		struct  People_Information*ptr = (struct  People_Information*)realloc(p->date, (p->capacity+ADD)*sizeof(struct  People_Information));

		if (ptr != NULL)
		{
			p->date = ptr;
			p->capacity += ADD;
			printf("success add the memory\n");
			return 1;
		}
		else
		{
			perror("check()");
               return 0;
		}
			
	}
	else
		return 1;
}
void Add(struct contact* con)
{
	assert(con);
	if (check(con) == 0)
	{
		return;
	}
		printf("Please input the name : \n");
		scanf("%s", con->date[con->sz].name);
		printf("PLease input the gender : \n");
		scanf("%s", con->date[con->sz].gender);
		printf("Please input the age : \n");
		scanf("%d", &con->date[con->sz].age);
		printf("Please input the telephone : \n");
		scanf("%s", con->date[con->sz].telephone);
		printf("Please input the adress : \n");
		scanf("%s", con->date[con->sz].adress);

		con->sz++;
		printf("success! \n");

}

void Save(struct contact* con)
{
	//open 
	FILE* pf=fopen("data.txt", "w");
	//write
	for (int i = 0; i < con->sz; i++)
	{
		fwrite(con->date + i, sizeof(struct People_Information), 1, pf);
	}

	//close
	fclose(pf);
	pf = NULL;
}

void Destory(struct contact*con)
{
	free(con->date);
	con->capacity = 0;
	con->sz = 0;
	con->date = NULL;
}

int find_name(struct contact* con, char* arr)
{
	int i = 0;
	for (i = 0; i < con->sz; i++)
	{
		if (strcmp(con->date[i].name, arr) == 0)
		{
			return i;
		}
	}
	return -1;
}
int find_gender(struct contact* con, char* arr)
{
	int i = 0;
	for (i = 0; i < con->sz; i++)
	{
		if (strcmp(con->date[i].gender, arr) == 0)
		{
			return i;
		}
	}
	return -1;
}
int find_age(struct contact* con, int a)
{
	int i = 0;
	for (i = 0; i < con->sz; i++)
	{
		if (strcmp(con->date[i].age, a) == 0)
		{
			return i;
		}
	}
	return -1;

}
int find_telephone(struct contact* con, char* arr)
{
	int i = 0;
	for (i = 0; i < con->sz; i++)
	{
		if (strcmp(con->date[i].telephone, arr) == 0)
		{
			return i;
		}
	}
	return -1;
}
int find_adress(struct contact* con, char* arr)
{
	int i = 0;
	for (i = 0; i < con->sz; i++)
	{
		if (strcmp(con->date[i].adress, arr) == 0)
		{
			return i;
		}
	}
	return -1;
}


void Del_name(struct contact* con)
{
	printf("Please input name you want to delete\n");
	char arr[20];
	scanf("%s", arr);
	int ret = find_name(con,arr);
	if (ret!=-1 )
	{
		printf("have fund the member\n");
		int i = 0;;
		for (i = ret; i < con->sz-1; i++)
		{
			con->date[i] = con->date[i + 1];
		}
		con->sz--;
		printf("success\n");
	}
	else
	{
		printf("Can not find the member,please input again:\n");

	}
}


void menu2(void)
{
	printf("****  1.name   2.gender    ****\n");
	printf("****  3.age    4.telephone ****\n");
	printf("****  5.adress             ****\n");

}
void Mod(struct contact* con)
{
	printf("Please input name you want to modifier\n");
	char arr[20];
	scanf("%s", arr);
	int ret = find_name(con, arr);
	if (ret!=-1)
	{
		printf("have fund the member ,please choose the information:\n");
		menu2();
		int a = 0;
		scanf("%d", &a);
		switch (a)
		{
		case 1:
		{
         printf("name :\n");
		scanf("%s", con->date[ret].name);
			break;
		}
		case 2:
		{
        printf("gender :\n");
		scanf("%s", con->date[ret].gender);
		break;
		}
		case 3:
		{
		printf("age :\n");
		scanf("%d", &con->date[ret].age);
		break;
		}
		case 4:
		{
		printf("telephone :\n");
		scanf("%s", con->date[ret].telephone);
		break;
		}
		case 5:
		{
        printf("adress :\n");
		scanf("%s", con->date[ret].adress);
		break;
		}
		default:
		{
			printf("error\n");
			return;
		}
		}
		printf("success\n");
	}
	else
	{
		printf("Can not find the member,please input again:\n");

	}
}

int com_name(void* p1, void* p2)
{
	return (strcmp(((struct People_Information*)p1)->name , ((struct People_Information*)p2)->name));
}
int com_gender(void* p1, void* p2)
{
	return (strcmp(((struct People_Information*)p1)->gender, ((struct People_Information*)p2)->gender));
}int com_age(void* p1, void* p2)
{
	return (strcmp(((struct People_Information*)p1)->age, ((struct People_Information*)p2)->age));

}
int com_telephone(void* p1, void* p2)
{
	return (strcmp(((struct People_Information*)p1)->telephone, ((struct People_Information*)p2)->telephone));

}
int com_adress(void* p1, void* p2)
{
	return (strcmp(((struct People_Information*)p1)->adress, ((struct People_Information*)p2)->adress));

}


void Sort(struct contact* con)
{
	menu2();
	printf("Please input the element you want to sort\n");
	int a = 0;
	scanf("%d", &a);
	switch (a)
	{
	case 1:
	{
		qsort(con->date, con->sz, sizeof(struct People_Information), com_name);

		break;
	}
	case 2:
	{
		qsort(con->date, con->sz, sizeof(struct People_Information), com_gender);

		break;
	}
	case 3:
	{
		qsort(con->date, con->sz, sizeof(struct People_Information), com_age);

		break;
	}
	case 4:
	{
		qsort(con->date, con->sz, sizeof(struct People_Information), com_telephone);

		break;
	}
	case 5:
	{
		qsort(con->date, con->sz, sizeof(struct People_Information), com_adress);

		break;
	}
	default:
	{
		printf("error\n");
		return;
	}
	}
	printf("success\n");

}

void Show(struct contact*con)
{
	printf("%-20s%-10s%-5s  %-20s%-20s\n", "name", "gender", "age", "telephone", "adress");
	for(int i=0;i<con->sz;i++)
	{
		printf("%-20s", con->date[i].name);
		printf("%-10s", con->date[i].gender);
		printf("%-5d", con->date[i].age);
		printf("%-20s", con->date[i].telephone);
		printf("%-20s\n", con->date[i].adress);
	}
	
}


void Search(struct contact* con)
{
	menu2();
	printf("Please input the element youo want to search: \n");
	int a = 0;
	scanf("%d", &a);
	switch (a)
	{
	case 1:
	{
		printf("Please input name\n");
		char arr[20];
		int ret=find_name(con,arr);
		if (ret != -1)
		{
			printf("Have fund!\n");
			printf("%-20s\n", con->date[ret].name);
			printf("%-6s\n", con->date[ret].gender);
			printf("%-5d\n", con->date[ret].age);
			printf("%-20s\n", con->date[ret].telephone);
			printf("%-20s\n", con->date[ret].adress);
			con->sz--;
		}
		break;
	}

	case 2:
	{
		printf("Please input gender\n");
		char arr[20];
		int ret = find_gender(con, arr);
		if (ret != -1)
		{
			printf("Have fund!\n");
			printf("%-20s", con->date[ret].name);
			printf("%-6s", con->date[ret].gender);
			printf("%-5d", con->date[ret].age);
			printf("%-20s", con->date[ret].telephone);
			printf("%-20s\n", con->date[ret].adress);
			con->sz--;
		}
		break;
	}
	case 3:
	{
		printf("Please input age\n");
		int a=0;
		int ret = find_age(con, a);
		if (ret != -1)
		{
			printf("Have fund!\n");
			printf("%-20s", con->date[ret].name);
			printf("%-6s", con->date[ret].gender);
			printf("%-5d", con->date[ret].age);
			printf("%-20s", con->date[ret].telephone);
			printf("%-20s\n", con->date[ret].adress);
			con->sz--;
		}

		break;
	}
	case 4:
	{
		printf("Please input telephone\n");
		char arr[20];
		int ret = find_telephone(con, arr);
		if (ret != -1)
		{
			printf("Have fund!\n");
			printf("%-20s", con->date[ret].name);
			printf("%-6s", con->date[ret].gender);
			printf("%-5d", con->date[ret].age);
			printf("%-20s", con->date[ret].telephone);
			printf("%-20s\n", con->date[ret].adress);
			con->sz--;
		}
		break;
	}
	case 5:
	{
		printf("Please input adress\n");
		char arr[20];
		int ret = find_adress(con, arr);
		if (ret != -1)
		{
			printf("Have fund!\n");
			printf("%-20s", con->date[ret].name);
			printf("%-6s", con->date[ret].gender);
			printf("%-5d", con->date[ret].age);
			printf("%-20s", con->date[ret].telephone);
			printf("%-20s\n", con->date[ret].adress);
			con->sz--;
		}
		break;
	}
	default:
	{
		printf("error\n");
		return;
	}
	}
	printf("success\n");
}

